/**
 *  JFTSTool - Java Full Text Search tool.
 *  Copyright (C) <2009>  <grupo JFTS>
 *  This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jftstool.fileParserFramework.pdfHandler;

import java.io.IOException;
import java.io.InputStream;

import org.jftstool.fileParserFramework.IDocumentHandler;
import org.jftstool.fileParserFramework.exceptions.DocumentHandlerException;
import org.jftstool.fileParserFramework.metadata.DocumentMetaData;
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.xml.sax.ContentHandler;

/**
 * Parser for PDF document.
 * @author grupo JFTS
 *
 */
public class PDFHandler implements IDocumentHandler{
	
	@Override
	public void getDocument(InputStream is, ContentHandler resultHandler, DocumentMetaData metaData) throws DocumentHandlerException {
		COSDocument document = null;
		
		document = _openDocument(is);
		
		try {
			PDDocument pdDoc = new PDDocument(document);
			_getMetaData(pdDoc, metaData);			
			PDFToResultHandler.pdfToText(pdDoc, resultHandler, metaData);
			pdDoc.close();
			document.close();
		}
		catch (IOException e) {
			_closeCOSDocument(document);
			throw new DocumentHandlerException("Error to read PDF document. Can't extract text");
		}
	}
	
	
	private void  _getMetaData(PDDocument pdDoc, DocumentMetaData metaData) throws DocumentHandlerException{
		PDDocumentInformation docInfo = pdDoc.getDocumentInformation();
		metaData.set(DocumentMetaData.AUTHOR, docInfo.getAuthor());
		metaData.set(DocumentMetaData.TITLE, docInfo.getTitle());
		metaData.set(DocumentMetaData.KEYWORDS, docInfo.getKeywords());		
		metaData.set(DocumentMetaData.SUBJECT, docInfo.getSubject());
		metaData.set(DocumentMetaData.CREATOR, docInfo.getCreator());
		
		try {
			metaData.set(DocumentMetaData.CREATION_DATE, docInfo.getCreationDate());
			metaData.set(DocumentMetaData.LAST_MODIFIED, docInfo.getModificationDate());			
		}
		catch (IOException e) { /* do nothing */ }
	}
	

	private COSDocument _openDocument(InputStream is) throws DocumentHandlerException{
		COSDocument document = null;		
		try {
			document = _parseDocument(is);			
			if (document.isEncrypted()){
				_closeCOSDocument(document);
				throw new DocumentHandlerException("Error to read PDF document. Document is encrypted");
			}
		}
		catch (IOException e) {
			e.printStackTrace();
			_closeCOSDocument(document);
			throw new DocumentHandlerException("Error to read PDF document");
		}
		return document;
	}	
	
	
	private COSDocument _parseDocument(InputStream is) throws IOException{
		PDFParser parser = new PDFParser(is);
		parser.parse();
		return parser.getDocument();		
	}
	
	
	private void _closeCOSDocument(COSDocument cosDoc) {
		if (cosDoc != null) {
			try {
				cosDoc.close();
				cosDoc = null;
			}
			catch (IOException e) {}
		}
	}
}